package cn.edu.sdjzu.knoverse.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.edu.sdjzu.knoverse.model.entity.kg.KnowledgeNode;
import cn.edu.sdjzu.knoverse.model.vo.kg.KnowledgeTreeVO;
import cn.edu.sdjzu.knoverse.model.vo.result.PageResult;
import cn.edu.sdjzu.knoverse.model.vo.result.Result;
import cn.edu.sdjzu.knoverse.model.vo.result.ResultCodeEnum;
import cn.edu.sdjzu.knoverse.service.KnowledgeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.stp.StpUtil;
import cn.edu.sdjzu.knoverse.utils.context.BaseContext;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/knowledge")
@RequiredArgsConstructor
@Tag(name = "知识点管理", description = "知识点相关接口")
public class KnowledgeController {

    private final KnowledgeService knowledgeService;

    @GetMapping
    @SaCheckPermission("knowledge::list")
    @Operation(summary = "获取知识点列表")
    public Result<PageResult<KnowledgeNode>> getKnowledgeList(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String summary,
            @RequestParam(required = false) Integer level,
            @RequestParam(required = false) Long courseId,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return Result.success(knowledgeService.getKnowledgeList(name, summary, level, courseId, currentPage, pageSize));
    }
    
    @GetMapping("/tree")
    @SaCheckPermission("knowledge::list-tree")
    @Operation(summary = "获取所有知识点树形结构数据")
    public Result<List<KnowledgeTreeVO>> getKnowledgeTree(@RequestParam(required = false) Long excludeKnowledgeId) {
        // 检查当前用户是否有权限获取所有课程的知识点
        boolean hasAllPermission = StpUtil.hasPermission("course::all");
        
        if (hasAllPermission) {
            // 有权限返回所有课程的知识点树
            return Result.success(knowledgeService.getKnowledgeTree(excludeKnowledgeId));
        } else {
            // 没有权限只返回自己教授的课程的知识点树
            Long userId = BaseContext.getUserId();
            return Result.success(knowledgeService.getKnowledgeTreeByTeacherId(userId, excludeKnowledgeId));
        }
    }
    
    @PostMapping("/course/{courseId}")
    @SaCheckPermission("knowledge::add")
    @Operation(summary = "创建知识点")
    public Result<KnowledgeNode> createKnowledge(
            @PathVariable Long courseId,
            @RequestBody KnowledgeNode knowledge) {
        return Result.success(knowledgeService.createKnowledge(knowledge, courseId), ResultCodeEnum.ADD_SUCCESS);
    }

    @PutMapping("/{id}")
    @SaCheckPermission("knowledge::update")
    @Operation(summary = "更新知识点")
    public Result<KnowledgeNode> updateKnowledge(
            @PathVariable Long id, 
            @RequestBody KnowledgeNode knowledge) {
        knowledge.setNodeId(id);
        return Result.success(knowledgeService.updateKnowledge(knowledge), ResultCodeEnum.UPDATE_SUCCESS);
    }

    @DeleteMapping("/{id}")
    @SaCheckPermission("knowledge::delete")
    @Operation(summary = "删除知识点")
    public Result<Object> deleteKnowledge(@PathVariable Long id) {
        knowledgeService.deleteKnowledge(id);
        return Result.success(null, ResultCodeEnum.DELETE_SUCCESS);
    }

    @GetMapping("/{id}")
    @SaCheckPermission("knowledge::detail")
    @Operation(summary = "获取知识点详情")
    public Result<KnowledgeNode> getKnowledge(@PathVariable Long id) {
        return Result.success(knowledgeService.getKnowledge(id));
    }
    
    @GetMapping("/course/{courseId}")
    @SaCheckPermission("knowledge::list-by-course")
    @Operation(summary = "获取课程的所有知识点")
    public Result<Set<KnowledgeNode>> getKnowledgeByCourse(@PathVariable Long courseId) {
        return Result.success(knowledgeService.getKnowledgeByCourse(courseId));
    }
    
    @PutMapping("/{knowledgeId}/prerequisites")
    @SaCheckPermission("knowledge::update-prerequisites")
    @Operation(summary = "更新知识点的前置知识点列表")
    public Result<Object> updateKnowledgePrerequisites(
            @PathVariable Long knowledgeId,
            @RequestBody List<Long> preKnowledgeIds) {
        knowledgeService.updatePreKnowledge(knowledgeId, preKnowledgeIds);
        return Result.success(null, ResultCodeEnum.UPDATE_SUCCESS);
    }
    
    @GetMapping("/{knowledgeId}/prerequisites")
    @SaCheckPermission("knowledge::get-prerequisites")
    @Operation(summary = "获取知识点的前置知识点列表")
    public Result<Set<KnowledgeNode>> getKnowledgePrerequisites(@PathVariable Long knowledgeId) {
        return Result.success(knowledgeService.getPreKnowledge(knowledgeId));
    }
} 